/**
 * 教师模块状态管理文件
 */

import { observable, makeAutoObservable, action, computed } from 'mobx';
import { storage } from './common';
const key = 'Shcool_Teacher';


class Teacher {
    constructor() {
        const value = storage.getItem(key);
        value && Object.assign(this, JSON.parse(value));
        makeAutoObservable(this);
    }

    /**
     * 教师信息
     */
    @observable
    teacher = {}

    /**
     * 设置教师信息
     * @param {*} teacher 
     */
    @action.bound
    setTeacher(teacher) {
        this.teacher = teacher;
        this.save();
    }

    /**
     * 设置token
     * @param {*} token 新的token
     */
    @action.bound
    setToken(token) {
        this.teacher.password = token;
        this.save();
    }

    /**
     * token
     */
    @computed
    get token() {
        return this.teacher?.password;
    }

    /**
     * 退出方法
     */
    @action.bound
    exit() {
        this.teacher = {};
        this.save();
        window.location.href = '/login';
    }

    /**
     * 保存（持久化）
     */
    save = () => {
        storage.setItem(key, JSON.stringify(this));
    }
}

export default new Teacher();